package com.ss.demo.springbootdemo;

import com.ss.demo.springbootdemo.controller.OrderController;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * 作者: MrZhangJR
 * 时间: 2021/11/3 14:57
 * 描述:
 */
public class CglibTest {


    public static void main(String[] args) {
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(OrderController.class);
        Callback callback = new MethodInterceptor() {
            @Override
            public Object intercept(Object proxyObj, Method targetMethod, Object[] targetArgs, MethodProxy methodProxy) throws Throwable {
                System.out.println("cglib代理开始代理喽....");
                Object o = methodProxy.invokeSuper(proxyObj, targetArgs);
                System.out.println("cglib代理完成...");
                return o;
            }
        };
        enhancer.setCallback(callback);

        OrderController o = (OrderController) enhancer.create();
        o.findOrderList(1,10);
        o.saveOrder(null);
    }

}
